#include "util.hpp"
#include "config.hpp"
#include "data.hpp"
#include"hot.hpp"
#include"service.hpp"
void test1() // 测试util.hpp获取文件属性接口
{
    cloud::Fileutil fu("hello.txt");
    std::cout << "size: " << fu.fileSize() << std::endl;
    int time = fu.fileAcctime();
    std::cout << "Acctime : " << time << std::endl;
    time = fu.fileModtime();
    std::cout << "Modtime : " << time << std::endl;
}

void test2() // 测试
{
    cloud::Fileutil fu("hello.txt");
    std::string body;
    fu.GetContent(&body);
    std::cout << "body " << body << std::endl;
    body += "test";
    fu.SetContent(body);
    fu.GetPosLen(0, body.size(), &body);
    std::cout << "body " << body << std::endl;
}

void test3() // 测试目录操作接口
{
    cloud::Fileutil fu("/home/hqy/Cloud/test");
    // cloud::Fileutil fu("./test");
    std::vector<std::string> vs;
    fu.ScanDirectory(&vs); // 内部会将test目录下的文件路径保存到vs中,是去除根目录的相对路径。
    for (auto &e : vs)
    {
        std::cout << e << std::endl;
    }
    cloud::Fileutil fu2("./a/b/c");
}
void test4() // 测试压缩和解压缩接口
{
    cloud::Fileutil fu("hello.txt");
    fu.Compress("hello.zip"); // 传入压缩文件名
    cloud::Fileutil nfu("hello.zip");
    nfu.UnCompress("hello2.txt"); // 传入解压文件名
}

void test5() // 测试util.hpp中的json接口
{
    Json::Value js;
    int arr[3] = {80, 90, 100};
    js["性别"] = "男";
    js["姓名"] = "hqy";
    js["成绩"].append(80);
    js["成绩"].append(90);
    js["成绩"].append(100);
    std::string st;
    cloud::Jsonutil::serialize(&st, js);
    // std::cout << "st " << st << std::endl;
    Json::Value js2;
    std::cout << "js " << &js2 << std::endl;
    cloud::Jsonutil::Unserialize(st, &js2); // 若st = "{"key":value}"->R("{"key":value}")使得内部""为普通字符
    std::cout << &js2["性别"] << std::endl;  // 返回的是Json::value对象,所以可以.访问类内函数,aString()，aInt().
    std::cout << js2["姓名"] << std::endl;
    std::cout << js2["成绩"] << std::endl;
}
void test6() // 测试单例配置信息类
{
    cloud::Config *cfg = cloud::Config::GetInstance();
    std::cout << cfg->GetHotime() << std::endl;
    std::cout << cfg->GetPort() << std::endl;
    std::cout << cfg->GetIp() << std::endl;
    std::cout << cfg->GetDprefix() << std::endl;
    std::cout << cfg->GetPackfile() << std::endl;
    std::cout << cfg->GetPacksuffix() << std::endl;
    std::cout << cfg->GetBackfile() << std::endl;
    std::cout << cfg->GetBackUpfile() << std::endl;
}
void test7() // 测试数据管理
{
    cloud::BackupInfo info;
    info.NewBackupInfo("./hello.txt");
    std::cout << info.flag_ << std::endl;
    std::cout << info.fsize_ << std::endl;
    std::cout << info.atime_ << std::endl;
    std::cout << info.mtime_ << std::endl;
    std::cout << info.url_ << std::endl;
    std::cout << info.pack_path_ << std::endl;
    std::cout << info.real_path_ << std::endl;
}
void test8() // 测试文件信息管理类
{
    cloud::BackupInfo info;
    info.NewBackupInfo("./hello.txt");
    cloud::DataManager* manager = cloud::DataManager::GetInstance();
    std::cout<<manager->Insert(info);
    std::cout<<manager->GetOnebyrealpath("./hello.txt", &info);
    std::cout << "-----------"<< "Insert GetOnebyrealpath" << std::endl;
    std::cout << info.flag_ << std::endl;
    std::cout << info.fsize_ << std::endl;
    std::cout << info.atime_ << std::endl;
    std::cout << info.mtime_ << std::endl;
    std::cout << info.url_ << std::endl;
    std::cout << info.pack_path_ << std::endl;
    std::cout << info.real_path_ << std::endl;

    std::cout << "-----------"<< " GetOnebyurl" << std::endl;
    std::cout<<manager->GetOnebyurl("/download/hello.txt", &info);
    info.flag_ = true;
    std::cout<<manager->Update(info);
    std::cout << info.flag_ << std::endl;
    std::cout << info.fsize_ << std::endl;
    std::cout << info.atime_ << std::endl;
    std::cout << info.mtime_ << std::endl;
    std::cout << info.url_ << std::endl;
    std::cout << info.pack_path_ << std::endl;
    std::cout << info.real_path_ << std::endl;

    std::cout << "-----------"<< " GetAll" << std::endl;
    std::vector<cloud::BackupInfo> vb;
    std::cout<<manager->GetAll(&vb);
    for (auto &e : vb)
    {
        std::cout << e.flag_ << std::endl;
        std::cout << e.fsize_ << std::endl;
        std::cout << e.atime_ << std::endl;
        std::cout << e.mtime_ << std::endl;
        std::cout << e.url_ << std::endl;
        std::cout << e.pack_path_ << std::endl;
        std::cout << e.real_path_ << std::endl;
    }
}
void test9() // 测试文件信息持久化
{
    cloud::DataManager* manager = cloud::DataManager::GetInstance();
    std::vector<cloud::BackupInfo> vb;
    std::cout<<manager->GetAll(&vb)<<std::endl;
    for (auto &e : vb)
    {
        std::cout << e.flag_ << std::endl;
        std::cout << e.fsize_ << std::endl;
        std::cout << e.atime_ << std::endl;
        std::cout << e.mtime_ << std::endl;
        std::cout << e.url_ << std::endl;
        std::cout << e.pack_path_ << std::endl;
        std::cout << e.real_path_ << std::endl;
    }
}
void test10()//测试热点模块
{
    cloud::HotManager hot;
    hot.RunModel();
}
void test11()//测试文件上传服务
{
    cloud::service sev;
    sev.RunModel();
}
void test12()
{
    DEBUG("%s","日志测试");
    INFO("%s","日志测试");
    WAR("%s","日志测试");
    ERROR("%s","日志测试");
    FATAL("%s","日志测试");
    OFF("%s","日志测试");
}
int main()
{
    std::thread threadHot(test10);
    std::thread threadserver(test11);
    threadHot.join();
    threadserver.join();
    // cloud::Fileutil fu("./packdir/1.docx.lz");
    // fu.Compress("2.docx");
    // test5();
    return 0;
}